﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemoteTestRunner.Contracts;

namespace RemoteTestRunner.BusinessLogic.Models
{
    internal static class RtrServiceResolver
    {

        internal static event EventHandler DataChanged;

        static RtrServiceResolver()
        {
            // Future resolutions
            RtrSettings.SettingsUpdated += new EventHandler(RtrSettings_SettingsUpdated);

            // Initial resolution
            RtrSettings_SettingsUpdated(null, EventArgs.Empty);
        }

        private static void  RtrSettings_SettingsUpdated(object sender, EventArgs e)
        {
            if (RtrSettings.UseRunnerService)
            {
                // Run on server
                Service = RemoteTestRunner.ServiceFacade.ServiceFactory.GetServiceObject(
                    RtrSettings.RunnerServiceUrl,
                    RtrSettings.TestTimeoutSeconds);
            }
            else
            {
                // Run on local client
                Service = RemoteTestRunner.ServiceFacade.ServiceFactory.GetServiceObject(
                    RemoteTestRunner.ServiceFacade.ServiceType.ClientService);
            }
            OnDataChanged(sender, e);
        }

        internal static IRTRService Service { get; private set; }

        internal static void OnDataChanged(object sender, EventArgs e)
        {
            if (DataChanged != null)
            {
                DataChanged(sender, e);
            }
        }

    }
}
